import { reqBuying, reqShow } from '@/api'
import { debounce} from 'lodash/function'
export default {
    state: {
        navList: [], // 轮播图数据
        showList: [], // 瀑布流数据
    },
    mutations: {
        WORTHBING(state, navList) {
            state.navList = navList
        },
        SHOWDATA(state, showList) {
            state.showList = showList
        },
        update(state,showList){
            state.showList.push(...showList)
        },
        // 清空瀑布流数据
        clearShowList(state){
            state.showList = []
        }
    },
    actions: {
        // 获取轮播图数据
        async getSwiperData({ state, commit, dispatch }) {
            let res = await reqBuying()
            if (res.code == 200) {
                commit('WORTHBING', res.data.navList)
                return 'ok'
            }
            return Promise.reject()
        },
        // 获取瀑布流数据并做防抖处理
        getShowData:debounce(async function({ state, commit, dispatch },page) {
            let res = await reqShow(page)
            if (res.code == 200) {
                // 如果已有数据则push
                if (state.showList.length) {
                    commit('update',res.data.result)
                    return
                }
                // 否则做第一次的存储
                commit('SHOWDATA', res.data.result)
                return 'ok'
            }
            return Promise.reject()
        },200),
        
    },
    getters: {
        navList1(state){
            let arr = [...state.navList];
            return arr.splice(0,2)
        },
        navList2(state){
            let arr = [...state.navList];
            return arr.splice(2,2)
        },
        navList3(state){
            let arr = [...state.navList];
            return arr.splice(4,2)
        },
        navList4(state){
            let arr = [...state.navList];
            return arr.splice(6,2)
        },
        navList5(state){
            let arr = [...state.navList];
            return arr.splice(8,2)
        },
        navList6(state){
            let arr = [...state.navList];
            return arr.splice(10,2)
        },
        navList7(state){
            let arr = [...state.navList];
            return arr.splice(12,2)
        }
    }
}